package com.atguigu.gulimall.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class gulimallCorsWebFilter {

    //自定义跨域的相关配置
    @Bean
    public CorsWebFilter corsWebFilter(){
        //基于URL进行跨域的
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许进行跨域的头
        corsConfiguration.addAllowedHeader("*");
        //允许进行跨域的方法
        corsConfiguration.addAllowedMethod("*");
        //允许跨域的来源
        //toDo: 在SpringBoot2.0以后，该配置改为了.setallowedOriginPattern();
//        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedOriginPattern("*");
        //是否允许携带cookie进行跨域
        corsConfiguration.setAllowCredentials(true);

        //配置任意的url都要进行跨域
        source.registerCorsConfiguration("/**",corsConfiguration);

        CorsWebFilter corsWebFilter = new CorsWebFilter(source);
        return corsWebFilter;
    }


}
